'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'], function ($) {
    return {
        init: function () {

          this.confirmed = false;
          this.initPage();
          this.initCreate();
          this.resetPassword();
          
        },

        initPage: function () {
          
          var that = this;
          
          $(document).on("change", 'input[name="loginName"]', function(e){
            that.confirmed = false;
          })

          $(document).on("shown.bs.modal", "#delete", function (e) {

              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              
              $form.shiftform({
                  beforeSubmit: function (el) {
                      $form.trigger("validate.ajax.submit");
                      return $.bt_validate.result[$.bt_validate.form_id]
                  }
              })

          });
        },
        
        initCreate: function () {
          
            var that = this;
            
            $(document).on("shown.bs.modal", "#create", function (e) {

                $(document).on('change', 'select[name="supplierId"]', function(el) {
                  var vam = $('select[name="supplierId"] option:selected').data('vam');
                  if (vam) {
                    
                    // 展示VAM需要选择框
                    $('.vam-user').show();
                    $('select[name="departmentId"]').data('validate', 'required');
                    
                    // 只显示选择公司的部门
                    var selectOrg = $('select[name="supplierId"] option:selected').data('org');
                    $('select[name="departmentId"] option').each(function() {
                      var org = $(this).data('org');
                      if (selectOrg == org) {
                        $(this).prop('disabled', false).removeClass('hide');
                      } else {
                        $(this).prop('disabled', true).addClass('hide');
                      }
                    });
                    
                  } else {
                    
                    $('.vam-user').hide();
                    $('select[name="departmentId"]').data('validate', '');
                    
                  }
                });
                
                $(document).on('change', 'select[name="departmentId"]', function(el) {
                  var departmentName = $('select[name="departmentId"] option:selected').text();
                  $('input[name="departmentName"]').val(departmentName);
                });
                
              
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

                var $form = $(e.currentTarget).find("form")                          //bind shiftform
                $form.shiftform({
                    beforeSubmit: function (el) {
                        
                        $form.trigger("validate.ajax.submit");
                        var result = $.bt_validate.result[$.bt_validate.form_id]
                        if (result) {
                          return that.confirmed ? true : that.validateLoginName();
                        } else {
                          return false;
                        }
                    }
                })
                
            });
            
        },
        
        validateLoginName: function() {
          var that = this;
          var loginName = $('input[name="loginName"]').val().trim();
          if (loginName === "" || loginName === undefined) {
            return false;
          }
          
          $.ajax({
            url: __CTX__.root + "/admin-users/exist-login-name",
            data: {
              loginName: loginName
            }
          }).done(function(res){
            
            res = typeof res === 'String' ? JSON.parse(res) : res;
            if (res.data.existLoginName) {

              var html = '<div class="tooltip fade bottom in" role="tooltip" id="login-name" style="display: block; top: 30px; left: 123.164px;"><div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div><div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);"><i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;该登录名已存在！<div id="tlt_1500628448810" <="" div=""></div></div></div>';
              $(".role-login-name").append(html);
              setTimeout("$('.login-name').remove();", 1000);
              return false;
              
            }
            
            that.confirmed = true;
            $("#create form button[type='submit']").click();
            
          }).fail(function(){
            return false;
          });
        },
        
        resetPassword: function() {
          $(document).on("shown.bs.modal", "#reset", function (e) {
            
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              
              $form.shiftform({
                  beforeSubmit: function (el) {
                      
                    var password = $('input[name="password"]').val().trim();
                    var confirmPassword = $('input[name="confirmPassword"]').val().trim();
                    
                    if (!(password === confirmPassword)) {
                      var html = '<div class="tooltip fade bottom in" role="tooltip" id="confirm-password" style="display: block; top: 30px; left: 89.8281px;"><div class="tooltip-arrow" style="left: 50%; border-bottom-color: rgb(235, 204, 209);"></div><div class="tooltip-inner" style="background-color: rgb(242, 222, 222); padding: 10px 12px; max-width: 500px; color: rgb(185, 74, 72);"><i class="glyphicon glyphicon-remove"></i>&nbsp;&nbsp;两次密码不一致！<div id="tlt_1500621070305" <="" div=""></div></div></div>';
                      $(".role-confirm-password").append(html);
                      setTimeout("$('#confirm-password').remove();", 1000);
                      return false;
                    }
                  
                    $form.trigger("validate.ajax.submit");
                    return $.bt_validate.result[$.bt_validate.form_id]
                      
                  }
              })
  
          });
        }
        
    }
})

